package com.example.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.example.domain.dto.DepartmentDTO;
import com.example.domain.po.Department;
import com.example.domain.po.Hospital;
import com.example.domain.vo.CourseVo;
import com.example.domain.vo.DepartmentVo;
import com.example.mapper.DepartmentMapper;
import com.example.service.DepartmentService;
import com.example.util.AliOSSUtils;
import com.example.util.PageBean;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Autowired
    private AliOSSUtils aliOSSUtils;

    @Override
    public List<Department> list() {
        return departmentMapper.list();
    }

    @Override
    public Department findById(Integer id) {
        return departmentMapper.findById(id);
    }

    @Override
    public List<Department> findByHospitalId(Integer id) {
        return departmentMapper.findByHospitalId(id);
    }

    @Override
    public PageBean<DepartmentVo> page(Integer pageNum, Integer pageSize, Integer hospitalId, String name) {

        PageBean<DepartmentVo> pb = new PageBean<>();
        PageHelper.startPage(pageNum, pageSize);
        List<DepartmentVo> list =  departmentMapper.page(name,hospitalId);
        Page<DepartmentVo> p = (Page<DepartmentVo>)list ;
        pb.setItems(p.getResult());
        pb.setTotal(p.getTotal());
        return pb;
    }

    @Transactional
    @Override
    public String save(DepartmentDTO departmentDTO) throws IOException {
        if (departmentDTO.getId()==null){
            departmentMapper.add(departmentDTO);
            return "添加成功";
        }else {
            departmentMapper.update(departmentDTO);
            return "修改成功";
        }

    }

    @Override
    public PageBean<Department> singleList(Integer pageNum, Integer pageSize, String name) {
        PageBean<Department> pb = new PageBean<>();
        PageHelper.startPage(pageNum, pageSize);
        List<Department> list = departmentMapper.singleList(name);
        Page<Department> p = (Page<Department>)list ;
        pb.setItems(p.getResult());
        pb.setTotal(p.getTotal());
        return pb;
    }

    @Override
    public void del(Integer id) {
        departmentMapper.del(id);
    }
}
